package com.czh.wx.handler;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caozaihui
 * @version 1.0
 * @since 2024/9/1 下午 9:30
 */
@Component
public class WxMsgFactory implements InitializingBean {

    @Resource
    List<WxMsgHandler> wxMsgHandlerList;
    private final Map<WxChatMsgTypeEnum, WxMsgHandler> wxMsgHandlerMap = new HashMap<>();

public WxMsgHandler getHandlerByMsgType(String msgType) {
    WxChatMsgTypeEnum wxChatMsgTypeEnum = WxChatMsgTypeEnum.getByMsgType(msgType);
    return wxMsgHandlerMap.get(wxChatMsgTypeEnum);
}
    @Override
    public void afterPropertiesSet() {
        wxMsgHandlerList.forEach(wxMsgHandler -> wxMsgHandlerMap.put(wxMsgHandler.getMsgType(), wxMsgHandler));
    }
}
